//
//  MainViewController.m
//  Conference
//
//  Created by Xiang He on 12-11-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "GroupViewController.h"
#import "ChatViewController.h"
#import "ShakeViewController.h"
#import "Constant.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "Member.h"
#import "WebData.h"
#import "MainDataBase.h"
#import "NetContant.h"
#import "HotelData.h"
#import "MessageService.h"
#import "Globals.h"
@interface MainViewController ()

@end

@implementation MainViewController
@synthesize txt_main_title;
@synthesize  groupTab;
@synthesize queue;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
   
    [super viewDidLoad];
    [self.txt_main_title setText:[[NSUserDefaults standardUserDefaults] objectForKey:[[Constant getInstance] MeetingName]]];
    
    self.queue=[[[NSOperationQueue alloc] init] autorelease];
    
    [[Globals getInstance] setIsThreadExit:NO];
    MessageService *messageService=[[[MessageService alloc] init] autorelease];
    
    [self.queue addOperation:messageService];
    
    NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
    [thread start];
    // Do any additional setup after loading the view from its nib.
}

-(void)run{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    
    [[MainDataBase getInstance] setUser];
    NSString *sRet=[[WebData getInstance]getHotelInfo];
    
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:sRet waitUntilDone:YES];
	[pool release];
    
}

-(void)onPostExecute:(NSString *)sRet{
    
    if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
  
        
    }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_RECORD_ISEMPTY]]){
        
    
        
    }else {
        @try {
            [self doNetRet:sRet];
        }
        @catch (NSException *exception) {
            
        }
        
        
    }
    
}

-(void)doNetRet:(NSString *)sRet{
    NSArray *tables=[sRet componentsSeparatedByString:[[NetContant getInstance]TABLES_SPLIT_FLAG]];
    if(nil!=[tables objectAtIndex:0]){
        NSArray *record=[[tables objectAtIndex:0]componentsSeparatedByString:[NetContant getInstance].RECORDS_SPLIT_FLAG];
        if([record count]>=11){
            HotelData *hoteldata=[[HotelData alloc]init];
            hoteldata.hotelName=[record objectAtIndex:0];
            hoteldata.phone = [record objectAtIndex:1];
            hoteldata.hotleAddress = [record objectAtIndex:2];
            hoteldata.cordX=[record objectAtIndex:3];
            hoteldata.cordY=[record objectAtIndex:4];
            hoteldata.roomType=[record objectAtIndex:5];
            hoteldata.checkTime=[record objectAtIndex:6];
            hoteldata.moveTime=[record objectAtIndex:7];
            hoteldata.bookHotelFlag=[record objectAtIndex:8];
            hoteldata.shareCustCompany=[record objectAtIndex:9];
            hoteldata.shareCustName=[record objectAtIndex:10];
            Member *member=[(AppDelegate *)[[UIApplication sharedApplication] delegate]userMember];
            member.hotelData=hoteldata;
            [hoteldata release];
            
        }
    }
}



- (void)viewDidUnload
{
    [self setTxt_main_title:nil];
    [self setQueue:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    
    [txt_main_title release];
    [queue release];
    [[Globals getInstance] setIsThreadExit:YES];
    [super dealloc];
}

-(void)intoGroup:(int)type{
    
    GroupViewController *mGroupViewController=[[GroupViewController alloc] initWithNibName:@"GroupViewController" bundle:nil];
    mGroupViewController.type=type;
    [self.navigationController pushViewController:mGroupViewController animated:YES];
    [mGroupViewController release];
}

- (IBAction)confNoticeAction:(id)sender {
    [self intoGroup:[[Constant getInstance]CONTROLLER_ALARM]];
}

- (IBAction)confScheduleAction:(id)sender {
    [self intoGroup:[[Constant getInstance]CONTROLLER_SCHEDULE]];

}

- (IBAction)confNewsAction:(id)sender {
     [self intoGroup:[[Constant getInstance]CONTROLLER_NEWS]];
}

- (IBAction)hotelReservationAction:(id)sender {
    [self intoGroup:[[Constant getInstance]CONTROLLER_HOTEL]];

}

- (IBAction)shuttleArrAction:(id)sender {
    [self intoGroup:[[Constant getInstance]CONTROLLER_AIRPORT]];

}

- (IBAction)electronicPassesAction:(id)sender {
    [self intoGroup:[[Constant getInstance]CONTROLLER_CREDENTIALS]];

}

- (IBAction)shakeAction:(id)sender {
    ShakeViewController *shakeController=[[ShakeViewController alloc]initWithNibName:@"ShakeViewController" bundle:nil];
    [self.navigationController pushViewController:shakeController animated:YES];
    [shakeController release];
}

- (IBAction)venueInfoAction:(id)sender {
    [self intoGroup:[[Constant getInstance]CONTROLLER_PALCE]];
    
}

- (IBAction)electronicSpeechAction:(id)sender {
    [self intoGroup:[[Constant getInstance]CONTROLLER_ELESPEECH]];
}

- (IBAction)activityArrAction:(id)sender {
    [self intoGroup:[[Constant getInstance]CONTROLLER_TASKASSIGN]];

}

- (IBAction)customerServiceAction:(id)sender {
    ChatViewController *chatController=[[ChatViewController alloc]initWithNibName:@"ChatViewController" bundle:nil];
    [self.navigationController pushViewController:chatController animated:YES];
    [chatController release];

}

- (IBAction)userCanceledAction:(id)sender {
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要注销系统" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是",nil];
    [alert show];
    [alert release];

}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex ==[alertView firstOtherButtonIndex]) {
        
        
    [self.navigationController popViewControllerAnimated:YES];
        
//     LoginViewController *loginViewController=[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
//     [self.navigationController pushViewController:loginViewController animated:YES];
//    [loginViewController release];
    
    }
}
@end
